package com.zhuozhou.jdyc.main.goods.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityGoodsListBinding;
import com.zhuozhou.jdyc.databinding.IncludeGoodsScreenBinding;
import com.zhuozhou.jdyc.main.goods.viewmodel.GoodsViewModel;
import com.zhuozhou.jdyc.main.main.adapter.BarterShopAdapter;
import com.zhuozhou.jdyc.main.main.adapter.IndexShopAdapter;
import com.zhuozhou.jdyc.main.main.adapter.IntegralShopAdapter;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;

/**
 * Create by Once_ChuXy on 2021/9/14
 * Describe:商品
 */
public class GoodsActivity extends BaseActivity<GoodsViewModel, ActivityGoodsListBinding> implements IndexShopAdapter.OnShopItemClickListener,
        IntegralShopAdapter.OnShopItemClickListener, BarterShopAdapter.OnShopItemClickListener {

    private IntegralShopAdapter integralShopAdapter;
    private BarterShopAdapter barterShopAdapter;
    private IndexShopAdapter indexShopAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_list;
    }

    @Override
    public String setTitleBar() {
        return "限时折扣";
    }

    @Override
    public void initView() {
        if (baseBundle.getInt("type", 1) == 1 || baseBundle.getInt("type", 1) == 3) {
            setThemeBarAndToolBarColor(R.color.themeColor);
            binding.llColor.setBackgroundColor(getResources().getColor(R.color.themeColor));
        } else {
            setThemeBarAndToolBarColor(R.color.theme_green);
            binding.llColor.setBackgroundColor(getResources().getColor(R.color.theme_green));
        }
        mViewModel.BasicsTopDetailsMutableLiveData.observe(this, v -> {
            indexShopAdapter = new IndexShopAdapter(this);
            indexShopAdapter.setList(v);
            indexShopAdapter.setOnShopItemClickListener(this);
            binding.mRecyclerViewShop.setAdapter(indexShopAdapter);
            binding.mRecyclerViewShop.setLayoutManager(new FullyGridLayoutManager(this, 2) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            switch (baseBundle.getInt("type")) {
                case 1:
                    indexShopAdapter = new IndexShopAdapter(this);
                    indexShopAdapter.setList(v);
                    indexShopAdapter.setOnShopItemClickListener(this);
                    binding.mRecyclerViewShop.setAdapter(indexShopAdapter);
                    binding.mRecyclerViewShop.setLayoutManager(new FullyGridLayoutManager(this, 2) {
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    break;
                case 2:
                    integralShopAdapter = new IntegralShopAdapter(this);
                    integralShopAdapter.setList(v);
                    integralShopAdapter.setOnShopItemClickListener(this);
                    binding.mRecyclerViewShop.setAdapter(integralShopAdapter);
                    binding.mRecyclerViewShop.setLayoutManager(new FullyGridLayoutManager(this, 2) {
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    break;
                case 3:
                    barterShopAdapter = new BarterShopAdapter(this);
                    barterShopAdapter.setList(v);
                    barterShopAdapter.setOnShopItemClickListener(this);
                    binding.mRecyclerViewShop.setAdapter(barterShopAdapter);
                    binding.mRecyclerViewShop.setLayoutManager(new FullyGridLayoutManager(this, 2) {
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    break;

            }
        });
        binding.searchEdit.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.searchEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                try {
                    mViewModel.getBasicsTopDetails("65", baseBundle.getInt("type") + "", 0, 1);
                    // 当按了搜索之后关闭软键盘
                    ((InputMethodManager) binding.searchEdit.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;//评价
        });
        binding.tvSearch.setOnClickListener(v -> {
            mViewModel.getBasicsTopDetails("65", baseBundle.getInt("type") + "", 0, 1);
        });
        IncludeGoodsScreenBinding screenBinding = binding.includeGoodsScreen;
        screenBinding.llTopPrice.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.VISIBLE);
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_up, 0);
            screenBinding.tvTopSynthesis.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
            screenBinding.tvTopPrice.setTextColor(this.getResources().getColor(R.color.themeColor));
            screenBinding.tvTopSynthesis.setTextColor(this.getResources().getColor(R.color.text_desc_color));
        });
        screenBinding.llTopSynthesis.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.llSynthesis.setVisibility(View.VISIBLE);
            screenBinding.tvTopSynthesis.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_up, 0);
            screenBinding.tvTopPrice.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
            screenBinding.tvTopSynthesis.setTextColor(this.getResources().getColor(R.color.themeColor));
            screenBinding.tvTopPrice.setTextColor(this.getResources().getColor(R.color.text_desc_color));
        });
        screenBinding.tvSynthesis.setOnClickListener(v -> {
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            mViewModel.getBasicsTopDetails(baseBundle.getInt("admin_id") + "", baseBundle.getInt("id") + "", 0, baseBundle.getInt("type"));
        });
        screenBinding.tvSales.setOnClickListener(v -> {
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSales.getText().toString());
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            mViewModel.getBasicsTopDetails(baseBundle.getInt("admin_id") + "", baseBundle.getInt("id") + "", 1, baseBundle.getInt("type"));
        });
        screenBinding.tvOnTheNew.setOnClickListener(v -> {
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopSynthesis.setText(screenBinding.tvOnTheNew.getText().toString());
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            mViewModel.getBasicsTopDetails(baseBundle.getInt("admin_id") + "", baseBundle.getInt("id") + "", 2, baseBundle.getInt("type"));
        });
        screenBinding.tvPrice.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            mViewModel.getBasicsTopDetails(baseBundle.getInt("admin_id") + "", baseBundle.getInt("id") + "", 0, baseBundle.getInt("type"));
        });
        screenBinding.tvPriceDescending.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setText(screenBinding.tvPriceDescending.getText().toString());
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            mViewModel.getBasicsTopDetails(baseBundle.getInt("admin_id") + "", baseBundle.getInt("id") + "", 3, baseBundle.getInt("type"));
        });
        screenBinding.tvPriceAscending.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setText(screenBinding.tvPriceAscending.getText().toString());
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            mViewModel.getBasicsTopDetails(baseBundle.getInt("admin_id") + "", baseBundle.getInt("id") + "", 4, baseBundle.getInt("type"));
        });

    }

    @Override
    public void initData(Bundle bundle) {
        toolbarConfig.setTitle(bundle.getString("name"));
        mViewModel.getBasicsTopDetails(bundle.getInt("admin_id") + "", bundle.getInt("id") + "", 0, baseBundle.getInt("type"));
    }

    @Override
    public void onShopItemClick(View view, int position) {
        Intent intent = new Intent(this, GoodsDetailActivity.class);
        Bundle bundle = new Bundle();
        switch (baseBundle.getInt("type")) {
            case 1:
                bundle.putString("id", indexShopAdapter.getList().get(position).getCommodity_id() + "");
                break;
            case 2:
                bundle.putString("id", integralShopAdapter.getList().get(position).getCommodity_id() + "");
                break;
            case 3:
                bundle.putString("id", barterShopAdapter.getList().get(position).getCommodity_id() + "");
                break;

        }
        bundle.putInt("type", baseBundle.getInt("type"));
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
